<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top:5%;height: 55%;overflow-y: hidden">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="定时任务名称">
          <a-input placeholder="请输入定时任务名称" v-decorator="[ 'jobName', validatorRules.jobName]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="类名称">
          <a-input placeholder="请输入类名称" v-decorator="[ 'className', validatorRules.className]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="方法">
          <a-input placeholder="请输入方法" v-decorator="[ 'method', validatorRules.method]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="cron表达式">
          <a-input placeholder="请输入cron表达式" v-decorator="[ 'cron', validatorRules.cron]" />
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import {addTask,editTask } from '@/api/api'
  import { cronValidate } from '@/utils/cron'

  export default {
    name: "TaskModal",
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
          jobName:{rules: [{ required: true, message: '请输入定时任务名称!' }]},
          className:{rules: [{ required: true, message: '请输入类名称!' }]},
          method:{rules: [{ required: true, message: '请输入方法!' }]},
          cron:{
            rules: [
              {required: true, message: '请输入角色名称!'},
              {validator: this.validateCron}
            ]}
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;

        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'jobName', 'className','method','cron'))
        });

      },
      close () {
        this.$emit('ok');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let formData = Object.assign(this.model, values);
            let obj;
            console.log(formData)
            if(!this.model.id){
              obj=addTask(formData);
            }else{
              obj=editTask(formData);
            }
            obj.then((res)=>{
              if(res.code == 200){
                that.$message.success(res.remark);
                that.$emit('ok');
                that.close();
              }else{
                that.$message.warning(res.remark);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
      validateCron(rule, value, callback){
        if(!cronValidate(value)){
          callback("cron表达式有误!");
        }else{
          callback();
        }
      }

    }
  }
</script>

<style scoped>

</style>